import Vue from 'vue'
import VueRouter from 'vue-router'
import Login from '@/views/Login'
import Reg from '@/views/Reg'
import Layout from '@/views/Layout'
import Artcategory from '@/views/Artcategory'
import ArtList from '@/views/ArtList'
import UserInfor from '@/views/UserInfor'
import ChangeAvatar from '@/views/ChangeAvatar'
import ResetPwd from '@/views/ResetPwd'
import Home from '@/views/Home'

Vue.use(VueRouter)

const router = new VueRouter({
  routes: [
    { path: '/login', component: Login },
    { path: '/reg', component: Reg },
    {
      path: '/',
      component: Layout,
      children: [
        {
          path: '/',
          component: Home
        },
        {
          path: '/artcategory',
          component: Artcategory
        },
        {
          path: '/artlist',
          component: ArtList
        },
        {
          path: '/userinfo',
          component: UserInfor
        },
        {
          path: '/changeavatar',
          component: ChangeAvatar
        },
        {
          path: '/resetpwd',
          component: ResetPwd
        }
      ]
    }
  ]
})
const whiteList = ['/login', '/reg']
router.beforeEach((to, from, next) => {
  if (whiteList.includes(to.path)) {
    next()
  } else {
    if (localStorage.getItem('token')) {
      next()
    } else {
      next('/login')
    }
  }
})
export default router
